module compute_gen
!DIR$ NOOPTIMIZE
	use basicmodule
	implicit none
	
	
	contains

	subroutine inititerate
		integer	:: ir,i,k

		s2dist=1.0/ns2grid
		CFs2dist=1.0/ns2grid
		CoCs2dist=1.0/ns2grid
		if(.not. trialrun) then
			f=0
			fm=0
			do ir=1,n
				associate(r=>region(ir))
					Xs(:,ir)=matmul(r%AApAi,r%y)
					Us(:,ir)=Xs(:,ir)-f
				end associate
			enddo
			Ydummy=0
			thdist=1.0/nth
			CFthdist=1.0/nth
			CoCthdist=1.0/nth
			sigma2_F=s2fmtab(1)
			sigma2_F(2)=sigma2_Faprior/sigma2_Fanuprior
			cindf=1
			muU=0.0
			om2=1
			kappa2=1
			clubs2=1
			CoCs2=1
			rhos=0.0
			CoCrhos=0.0
			do i=1,nclubs
				cindCF(i)=mod(i-1,nth)+1
				Cfs(:,i)=0.0
				CoCid(i)=mod(i-1,nCoCs)+1
			enddo
			do ir=1,n
				clubid(ir)=mod(ir-1,nclubs)+1
				cindu(ir)=cindCF(clubid(ir))
			enddo
			do k=1,nCoCs
				cindCOC(k)=mod(k-1,nth)+1
				CoCFs(:,k)=0
			enddo
			rhodist=1.0/nrhogrid
			CoCrhodist=1.0/nrhogrid
			
		else
			mt_f=[3,7]
			Ydummy=0
			thdist=1.0/nth
			CoCthdist=1.0/nth
			CFthdist=1.0/nth
			rhodist=1.0/nrhogrid
			CoCrhodist=1.0/nrhogrid
			sigma2_F=s2fmtab(1)
			sigma2_F(2)=sigma2_Faprior/sigma2_Fanuprior
			cindf=3
			om2=om2prior/om2nuprior; kappa2=1; clubs2=1; CoCs2=1.0
			clubs2=[(i,i=1,nclubs)]
			kappa2=[(i,i=1,n)]
			muU=10
			rhos=0.7
			do i=1,nclubs
				cindCF(i)=mod(i-1,nth)+1
				Cfs(:,i)=0.0
				call rnnoa(CFs(:,i))
				CoCid(i)=mod(i-1,nCoCs)+1
			enddo
			do ir=1,n
				clubid(ir)=mod(17*ir,nclubs)+1
				cindu(ir)=mod(ir+23,nth)+1
			enddo
			call rnun(CoCrhos)
			CoCrhos=CoCrhos*0.9
!			CoCrhos=0.0
			do k=1,nCoCs
				cindCOC(k)=mod(k+3,nth)+1
				CoCFs(:,k)=0
			enddo
			print *,"cindCF,cindCOC"
			call mdisp(real(cindCF))
			call mdisp(real(cindCOC))
			
			f=0
			fm=0
			Xs=0
			Us=0
			do ir=1,n
				region(ir)%Y=0
			enddo	
		endif
	end subroutine
	
end module
